<template>
  <!-- <div> -->
  <el-container class="wrap">
    <!-- 头部 -->
    <el-header>
      <div class="logo-box">
        <img src="@/assets/uploads/logo.png" width="60" />
        <span>商城后台管理系统</span>
      </div>
      <el-button type="info" @click="logOut()">退出</el-button>
    </el-header>
    <!-- 头部 -->

    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="asideShow ? '64px' : '230px'">
        <!-- 切换侧边栏状态 -->
        <div class="tog-menu" @click="asideShow = !asideShow">|||</div>
        <!-- 切换侧边栏状态 -->
        <el-menu
          :default-openeds="openeds"
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse="asideShow"
          :collapse-transition="false"
          router
        >
          <el-submenu
            :index="item.id + ''"
            v-for="(item, idx) in menusList"
            :key="idx"
          >
            <template slot="title">
              <i :class="iconsList[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="(subItem, cIdx) in item.children"
              :key="cIdx"
              :route="{path: '/' + subItem.path}"
            >
              <template slot="title">
                <i class="el-icon-setting"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 侧边栏 -->

      <!-- 主要内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
      <!-- 主要内容 -->
    </el-container>
  </el-container>
  <!-- </div> -->
</template>

<script>
export default {
  data: () => {
    return {
      menusList: [],
      // 侧边栏标题图标映射表
      iconsList: {
        125: "iconfont icon-yonghu",
        103: "iconfont icon-quanxian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-icon-test",
        145: "iconfont icon-shuju",
      },
      // 侧边栏展开/收缩
      asideShow: false,
      // 侧边栏默认展开
      openeds: ["125"],
      activeId: "110"
    };
  },
  methods: {
    // 退出登录
    logOut() {
      // 跳转会登录页
      this.$router.push("/");
      // 清空token
      window.sessionStorage.clear();
    },

    // 获取左侧菜单列表
    async getUserList() {
      let { data: res } = await this.$http.get("/menus");
      if (res.meta.status != 200) return this.$message.error(res.meta.msg);
      this.menusList = res.data;
    },
  },

  created() {
    this.getUserList();
  },
};
</script>

<style scoped>
.wrap {
  height: 100vh;
}
.logo-box {
  display: flex;
  align-items: center;
  color: white;
}
.el-header {
  background-color: #666;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-aside {
  background-color: #333744;
}
.el-submenu__title > .iconfont {
  margin-right: 8px;
}
.el-aside .el-menu {
  border-right: 0;
}

.el-main {
  background-color: #eaedf1;
}

.tog-menu{
  background-color: grey;
  line-height: 26px;
  text-align: center;
  cursor: pointer;
}
</style>

<style>
.el-submenu__title {
  font-size: 16px;
}
</style>